package com.example.elasticsearch.controller;

import com.example.elasticsearch.domain.SelectRequest;
import com.example.elasticsearch.domain.User;
import com.example.elasticsearch.utils.ElasticsearchUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：wangshimin
 * @date ：Created in 2021-01-04 下午 03:24
 * @description：
 * @version:
 */
@RestController
public class EsController {

    @PostMapping("/createIndex")
    public boolean createIndex(String index) {
        try {
            return ElasticsearchUtil.createIndex(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @PostMapping("/deleteIndex")
    public boolean deleteIndex(String index) {
        try {
            return ElasticsearchUtil.deleteIndex(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @PostMapping("/isIndexExist")
    public boolean isIndexExist(String index) {
        try {
            return ElasticsearchUtil.isIndexExist(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @PostMapping("/addData")
    public String addData(String index, User user) {
        try {
            return ElasticsearchUtil.addData(user, index);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/bulkPostData")
    public boolean bulkPostData(String index) {
        List<User> userList = new ArrayList<>(1000);
        User user;
        for (int i = 0; i < 1000; i++) {
            user = new User();
            user.setAge(i);
            user.setName("王士敏" + i);
            user.setAddress("山东省滨州市沾化区" + i);
            userList.add(user);
        }
        return ElasticsearchUtil.bulkPost(index, userList);
    }

    @PostMapping("/deleteDataById")
    public boolean deleteDataById(String index, String id) {
        try {
            return ElasticsearchUtil.deleteDataById(index, id);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @PostMapping("/updateDataById")
    public boolean updateDataById(String index, User user, String id) {
        try {
            return ElasticsearchUtil.updateDataById(user, index, id);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @PostMapping("/searchDataById")
    public Map searchDataById(String index, String id, String fields) {
        try {
            return ElasticsearchUtil.searchDataById(index, id, fields);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new HashMap(0);
    }

    @PostMapping("/searchListData2")
    public List<Map<String, Object>> searchListData2(@RequestBody SelectRequest selectRequest) {
        Map<String, Object> fieldMap = selectRequest.getFieldMap();
        Map<String, Boolean> sortMap = selectRequest.getSortMap();
        try {
            return ElasticsearchUtil.queryPageByConditions(selectRequest.getIndex(), fieldMap, sortMap, selectRequest.getPageNum(), selectRequest.getPageSize(),
                    selectRequest.getHighlightField(), selectRequest.isRequireFieldMatch(), selectRequest.getPreTags(), selectRequest.getPostTags());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ArrayList<>(0);
    }
}
